<?php
namespace app\beautiful\controller\admin;
use app\beautiful\model\LevelModel;
use app\beautiful\model\member\MemberModel;
use app\BaseController;
use app\common\logic\DataLogic;

class Level extends BaseController{
    protected $name='会员等级';

    public function index()
    {
        $where=[
            ['is_delete','=',0],
            ['shop_id','=',$this->request->shopId]
        ];

        $levelModel=new LevelModel();
        $order=['vip_level'=>'asc'];
        $field="*";
        $append=['add_time_format'];
        $return=DataLogic::getDataList($levelModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $level_id=$this->request->param('level_id/d',0);
       if($level_id==0){
            $vip_levels=LevelModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0]
                ])->column('vip_level') ?? [];
            if(count($vip_levels) >=5){
                return $this->error($this->name.'已超过五个，无法添加了');
            }
            return $this->success(['vip_levels'=>$vip_levels]);
        }
        if(null === $level = LevelModel::find($level_id)){
            return $this->error($this->name.'不存在');
        }

        if($level->is_delete !=0){
            return $this->error($this->name.'不存在');
        }

        if($level->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        $detail=[
            'name'=>$level->name,
            'vip_level'=>$level->vip_level,
            'need_integral'=>$level->need_integral,
            'need_recharge'=>$level->need_recharge,
            'discount'=>$level->discount,
        ];

        $vip_levels=LevelModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
                ['vip_level','<>',$level->vip_level]
            ])->column('vip_level') ?? [];
        return $this->success(['detail'=>$detail,'vip_levels'=>$vip_levels]);
    }

    public function save()
    {
        $level_id=$this->request->param('level_id/d',0);
        $name=$this->request->param('name');
        $vip_level=$this->request->param('vip_level/d');
        $need_integral=$this->request->param('need_integral/d',0);
        $need_recharge=$this->request->param('need_recharge/d',0);
        $discount=$this->request->param('discount/f',0);
        if(empty($name) || mb_strlen($name) >10){
            return $this->error($this->name.'名称必须填写且长度不能多余10');
        }
        if(empty($vip_level) || !in_array($vip_level,[1,2,3,4,5])){
            return $this->error('请选择'.$this->name);
        }
        if(empty($need_integral) || $need_integral<=0){
            return $this->error('请填写需要达到的积分，且积分不能小于0');
        }
        if(empty($need_recharge) || $need_recharge<=0){
            return $this->error('请填写需要达到的充值数，且充值数不能小于0');
        }
        if(empty($discount) || $discount<=0){
            return $this->error('请填写优惠折扣，且折扣不能小于0');
        }

        //与低一级进行判断
        $down_level=LevelModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['vip_level','<',$vip_level],
            ['level_id','<>',$level_id]
        ])->order(['vip_level'=>'desc'])->find();
        if(null !== $down_level){
            if($need_integral < $down_level->need_integral){
                return $this->error('达到的积分不能小于低一级的积分数');
            }
            if($need_recharge < $down_level->need_recharge){
                return $this->error('达到的充值数不能小于低一级的充值数');
            }
            if($discount > $down_level->discount){
                return $this->error('优惠折扣不能高于于低一级的优惠折扣');
            }
        }

        //与高一级的进行判断
        $up_level=LevelModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['vip_level','>',$vip_level],
            ['level_id','<>',$level_id]
        ])->order(['vip_level'=>'asc'])->find();
        if(null !== $up_level){
            if($need_integral > $up_level->need_integral){
                return $this->error('达到的积分不能高于高一级的积分数');
            }
            if($need_recharge > $up_level->need_recharge){
                return $this->error('达到的充值数不能高于高一级的充值数');
            }
            if($discount < $up_level->discount){
                return $this->error('优惠折扣不能低于于高一级的优惠折扣');
            }
        }

        if($level_id!=0){
            if(null === $level =LevelModel::find($level_id)){
                return $this->error($this->name.'不存在');
            }
            if($level->is_delete !=0){
                return $this->error($this->name.'不存在');
            }
            if($level->shop_id != $this->request->shopId){
                return $this->error($this->name.'不存在');
            }
        }else{
            $level_count=LevelModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
            ])->count() ?? 0;
            if($level_count>=5){
                return $this->error('只能添加五个');
            }
            $level = new LevelModel();
            $level->shop_id=$this->request->shopId;
        }

        $level->name=$name;
        $level->vip_level=$vip_level;
        $level->need_integral=$need_integral;
        $level->need_recharge=$need_recharge;
        $level->discount=$discount;
        $level->save();
        return $this->success('编辑成功');
    }

    public function del()
    {
        $level_id=$this->request->param('level_id/d',0);
        if(null === $level =LevelModel::find($level_id)){
            return $this->error($this->name.'不存在');
        }
        if($level->is_delete !=0){
            return $this->error($this->name.'不存在');
        }
        if($level->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        if($level->is_base ==1){
            return $this->error('系统自带的三个不可删除');
        }
        if(null !== MemberModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_delete','=',0],
                ['level_id','=',$level_id]
            ])->find()){
            return $this->error('会员等级底下还有会员，暂不可删除');
        }

        $level->is_delete=1;
        $level->save();
        return $this->success('删除成功');
    }
}
